Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Find 2 max in array' - 1 code snippet(s) found

 Sample 1. Write a Program to find two maximum numbers in an array

public class FindTwoMax {
public static void main(String[] args) {
int myArray[] = { 1, 3, 5, 8, 6, 3 };
int max1 = myArray[0];
int max2 = 0;
for (int count = 0; count < myArray.length; count++) {
if (max2 < myArray[count]) {
max2 = max1;
max1 = myArray[count];
} else if (max2 < myArray[count]) {
max2 = myArray[count];
}
}
System.out.println(max1);
System.out.println(max2);
}
}

   Like      Feedback     find 2 max in array



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner